package com.beimihu.leetcode;

import java.util.HashMap;
import java.util.Map;

public class WordPattern {
    public boolean wordPattern(String pattern, String s) {
        String[] list = s.split("");
        if(list.length != pattern.length()){
            return false;
        }

        HashMap<Character,String> c2s = new HashMap<>();
        HashMap<String,Character> s2c = new HashMap<>();
        for(int i = 0 ; i <list.length;i++){
            if(c2s.containsKey(pattern.charAt(i))){
                if( !c2s.get(pattern.charAt(i)).equals(list[i])){
                    return false;
                }
            }
            if(s2c.containsKey(list[i])){
                if( !s2c.get(list[i]).equals(pattern.charAt(i))){
                    return false;
                }
            }
            c2s.put(pattern.charAt(i),list[i]);
            s2c.put(list[i],pattern.charAt(i) );
        }
        return true;
    }
}
